################################################################################
# Replication script for table 1
# The populist radical right as memory entrepreneur? The prominence, sentiment, and interpretations of history in the German parliament
# British Journal of Political Science
################################################################################
rm(list = ls())

setwd("C:/Users/fkrawatzek/Dropbox/AfD/Writings/2022 National analysis/BJPS submission/BJPS_revision/dataverse")

# Load/Install necessary packages
pacman::p_load(MASS, stargazer, openxlsx)

MemorySentimentsDF <- read.xlsx("./Memory sentiments.xlsx")
TemporalSentimentsDF <- read.xlsx("./Temporal sentiments.xlsx")
RightWingSentimentsDF <- read.xlsx("./Right Wing sentiments.xlsx")
BaselineSentimentsDF <- read.xlsx("./Baseline sentiments.xlsx")

################################################################################
# Negative binomial glm regression
#----------------------------------------------------------------------------------
# Memory
#---------
Memory_pos <- glm.nb(extracterms ~ genderMaleDummyYes + ageAtSpeech + Party + region + TotalWords,
                                               data = MemorySentimentsDF)
summary(Memory_pos)

Memory_neg <- glm.nb(negterms ~ genderMaleDummyYes + ageAtSpeech +  Party + region + TotalWords,
                                            data = MemorySentimentsDF)
summary(Memory_neg)

# Temporal
#---------
Temporal_pos <- glm.nb(extracterms ~ genderMaleDummyYes + ageAtSpeech + Party + region + TotalWords,
                                                 data = TemporalSentimentsDF)
summary(Temporal_pos)

Temporal_neg <- glm.nb(negterms ~ genderMaleDummyYes + ageAtSpeech + Party + region + TotalWords,
                                              data = TemporalSentimentsDF)
summary(Temporal_neg)

# Right Wing
#-----------
RightWing_pos <- glm.nb(extracterms ~ genderMaleDummyYes + ageAtSpeech + Party + region + TotalWords,
                                                  data = RightWingSentimentsDF)
summary(RightWing_pos)

RightWing_neg <- glm.nb(negterms ~ genderMaleDummyYes + ageAtSpeech + Party + region + TotalWords,
                                               data = RightWingSentimentsDF)
summary(RightWing_neg)

# Baseline
#-----------
Baseline_pos <- glm.nb(extracterms ~ genderMaleDummyYes + ageAtSpeech + Party + region + TotalWords,
                                                 data = BaselineSentimentsDF)
summary(Baseline_pos)

Baseline_neg <- glm.nb(negterms ~ genderMaleDummyYes + ageAtSpeech + Party + region + TotalWords,
                                              data = BaselineSentimentsDF)
summary(Baseline_neg)

################################################################################
# Save Regression Output
#-----------------------
Models <- list(Baseline_pos, Baseline_neg,
               Memory_pos, Memory_neg, 
               Temporal_pos, Temporal_neg, 
               RightWing_pos, RightWing_neg)

#save model output
stargazer(Models, p.auto=FALSE,
          star.cutoffs = c(0.05, 0.01, 0.001), #apply.coef=exponentiate,
          column.labels   = c("Baseline - Positive Sentiment", "Baseline - Negative Sentiment",
                              "Memory - Positive Sentiment", "Memory - Negative Sentiment",
                              "Temporal - Positive Sentiment", "Temporal - Negative Sentiment",
                              "Right Wing - Positive Sentiment", "Right Wing - Negative Sentiment"),model.names=TRUE,
          digits=3, type="html", title = "Sentiment analysis",
          out="./Table 1 - unformatted.doc")


